package sg.edu.nus.iss.vmcs.simulation;

import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionListener;
import sg.edu.nus.iss.vmcs.abstraction.ISimulationControlPanel;


/*
 * @author Nyi Nyi Than
 */
public class SimulationControlPanel extends Frame implements
		ISimulationControlPanel
{
	private static final long serialVersionUID = 1L;
	
	private static final int NUM_BUTTONS = 5;
	private static final String title = "Simulation Control Panel";
	private static final String L_SIMUL_BEGIN = "Begin Simulation";
	private static final String L_SIMUL_END = "End Simulation";
	private static final String L_ACT_CUSTOMER = "Activate Customer Panel";
	private static final String L_ACT_MAINTAINER = "Activate Maintainer Panel";
    private static final String L_ACT_MACHINERY  = "Activate Machinery Panel";
	
	private Button buttons[] = new Button[NUM_BUTTONS];
	private Font titleFont = new Font("Helvetica", Font.BOLD, 18);
	private Font buttonFont = new Font("Helvetica", Font.BOLD, 14);

	private SimulationControlPanelPresenter presenter;
	
	public SimulationControlPanel(SimulationControlPanelPresenter presenter)
	{
		this.presenter = presenter;		
		
		this.setTitle(title);
		setLayout(new GridLayout(0, 1));
		add(createPanelLabel());

		addButton(SIMUL_BEGIN, L_SIMUL_BEGIN,this.presenter.getBeginSimulationListener());
		addButton(ACT_CUSTOMER, L_ACT_CUSTOMER,this.presenter.getActivateCustomerListener());
		addButton(ACT_MAINTAINER, L_ACT_MAINTAINER,this.presenter.getActivateMaintainerListener());
		addButton(ACT_MACHINERY, L_ACT_MACHINERY,this.presenter.getActivateMachineryListener());
		addButton(SIMUL_END, L_SIMUL_END,this.presenter.getEndSimulationListener());
		
		
		pack();
		setLocation(100, 100);
		
		addWindowListener(this.presenter.getClosingListener());
	}

	private Label createPanelLabel()
	{
		Label l = new Label(title);
		l.setBackground(Color.blue);
		l.setForeground(Color.white);
		l.setFont(titleFont);
		return (l);
	}

	private void addButton(int id, String label, ActionListener listener)
	{
		Button b = new Button(label);
		b.setBackground(Color.white);
		b.setForeground(Color.blue);
		b.setFont(buttonFont);
		b.addActionListener(listener);
		buttons[id] = b;
		add(b);
	}

	@Override
	public void setSimulationActive(boolean isOn)
	{
		setButtonState(SIMUL_BEGIN, !isOn);
		setButtonState(ACT_CUSTOMER, isOn);
		setButtonState(ACT_MAINTAINER, isOn);
		setButtonState(ACT_MACHINERY, isOn);
		setButtonState(SIMUL_END, isOn);
	}
	
	@Override
	public void setButtonState(int id, boolean state)
	{		
		buttons[id].setEnabled(state);
	}

	@Override
	public void display()
	{	
		this.setVisible(true);
	}

	@Override
	public void close()
	{
		dispose();
	}

	
}
